package locale

import (
	"fmt"
	"github.com/nicksnyder/go-i18n/v2/i18n"
	"golang.org/x/text/language"
	"gopkg.in/yaml.v3"
	"os"
	"path/filepath"
)

type Locale struct {
	Bundle *i18n.Bundle
}

func Init(path string) {
	bundle := i18n.NewBundle(language.SimplifiedChinese)
	bundle.RegisterUnmarshalFunc("yaml", yaml.Unmarshal)
	dir := fmt.Sprintf("%s/locale", path)
	files, err := os.ReadDir(dir)
	if err != nil {
		fmt.Println(err.Error())
	} else {
		for _, value := range files {
			if filepath.Ext(value.Name()) == ".yaml" {
				filename := fmt.Sprintf("%s/%s", dir, value)
				bundle.LoadMessageFile(filename)
			}
		}
	}
}
